/**
 * 模块加载器
 * 按顺序加载所有模块
 */
(function() {
    'use strict';
    
    const modules = [
        'static/js/modules/cart.js',
        'static/js/modules/product.js',
        'static/js/modules/transaction.js',
        'static/js/modules/store.js',
        'static/js/modules/system.js',
        'static/js/modules/language.js',
        'static/js/modules/ui.js'
    ];
    
    let loadedCount = 0;
    const totalModules = modules.length;
    
    function loadModule(src) {
        return new Promise((resolve, reject) => {
            const script = document.createElement('script');
            script.src = src;
            script.async = false;
            script.onload = () => {
                loadedCount++;
                console.log(`模块加载完成: ${src} (${loadedCount}/${totalModules})`);
                resolve();
            };
            script.onerror = () => {
                console.error(`模块加载失败: ${src}`);
                reject(new Error(`Failed to load module: ${src}`));
            };
            document.head.appendChild(script);
        });
    }
    
    function loadAllModules() {
        const promises = modules.map(module => loadModule(module));
        return Promise.all(promises);
    }
    
    // 等待DOM和翻译加载完成后再加载模块
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', function() {
            // 等待翻译加载
            if (window.onTranslationsLoaded) {
                window.onTranslationsLoaded(() => {
                    loadAllModules().then(() => {
                        // 加载主应用
                        if (typeof loadMainApp === 'function') {
                            loadMainApp();
                        } else {
                            // 直接加载main.js
                            const mainScript = document.createElement('script');
                            mainScript.src = 'static/js/main.js';
                            document.head.appendChild(mainScript);
                        }
                    });
                });
            } else {
                // 如果翻译加载器不存在，直接加载模块
                loadAllModules().then(() => {
                    const mainScript = document.createElement('script');
                    mainScript.src = 'static/js/main.js';
                    document.head.appendChild(mainScript);
                });
            }
        });
    } else {
        // DOM已经加载完成
        if (window.onTranslationsLoaded) {
            window.onTranslationsLoaded(() => {
                loadAllModules().then(() => {
                    const mainScript = document.createElement('script');
                    mainScript.src = 'static/js/main.js';
                    document.head.appendChild(mainScript);
                });
            });
        } else {
            loadAllModules().then(() => {
                const mainScript = document.createElement('script');
                mainScript.src = 'static/js/main.js';
                document.head.appendChild(mainScript);
            });
        }
    }
    
})();

